package com.tekj.cxqcb.view.Module.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.operation.BusinessDao;
import com.tekj.cxqcb.operation.resultBean.ApiShopServiceSelectAllBean;
import com.tekj.cxqcb.operation.resultBean.PubStatusBean;
import com.tekj.cxqcb.view.Module.activity.StoreModule.AddStoreActivity;
import com.tekj.cxqcb.view.Module.adapter.NotShelfGoodsAdapter;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.operationHelper.Bean.BindingViewBean;
import common.tool.Tools;
import es.dmoral.toasty.Toasty;

/**
 * 未上架商品列表
 */
public class NotShelfGoodsActivity extends BaseActivity {


    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.tv_search_num)
    TextView tvSearchNum;
    @BindView(R.id.ll_search_result)
    LinearLayout llSearchResult;
    @BindView(R.id.rv_goods)
    SwipeRecyclerView rvGoods;

    NotShelfGoodsAdapter adapter;
    BusinessDao businessDao;
    String id, type1, type2, name1, name2;

    @Override
    public void init() {
        tvTitle.setText("商品列表");
        businessDao = new BusinessDao(activity, this);

        id = getIntent().getStringExtra("id");
        type1 = getIntent().getStringExtra("type1");
        type2 = getIntent().getStringExtra("type2");
        name1 = getIntent().getStringExtra("name1");
        name2 = getIntent().getStringExtra("name2");


        adapter = new NotShelfGoodsAdapter(null);
        rvGoods.setLayoutManager(new LinearLayoutManager(activity));
        SwipeMenuCreator mSwipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                SwipeMenuItem deleteItem1 = new SwipeMenuItem(activity)
                        .setBackgroundColor(getResources().getColor(R.color.colorPrimary))
                        .setText("上架").setTextSize(13)
                        .setHeight(Tools.dip2Px(activity, 75))
                        .setWidth(Tools.dip2Px(activity, 50))
                        .setTextColor(getResources().getColor(R.color.white));
                rightMenu.addMenuItem(deleteItem1);

                SwipeMenuItem deleteItem2 = new SwipeMenuItem(activity)
                        .setBackgroundColor(getResources().getColor(R.color.red))
                        .setText("删除").setTextSize(13)
                        .setHeight(Tools.dip2Px(activity, 75))
                        .setWidth(Tools.dip2Px(activity, 50))
                        .setTextColor(getResources().getColor(R.color.white));
                rightMenu.addMenuItem(deleteItem2);
                // 注意：哪边不想要菜单，那么不要添加即可。
            }
        };

        rvGoods.setSwipeMenuCreator(mSwipeMenuCreator);

        // 设置监听器。
        // 菜单点击监听。
        OnItemMenuClickListener mItemMenuClickListener = new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, final int position) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();

                // 左侧还是右侧菜单：
                int direction = menuBridge.getDirection();
                if (direction == -1) {
                    // 菜单在Item中的Position：
                    switch (menuBridge.getPosition()) {
                        case 0:
                            Tools.showNormalDialogOne(activity, "上架", "是否将该商品上架"
                                    , new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            businessDao.ApiShopServiceUpdate(adapter.getData().get(position).getId(), 1);
                                        }
                                    }, null);
                            break;
                        case 1:
                            Tools.showNormalDialogOne(activity, "删除", "是否将该商品删除"
                                    , new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            businessDao.ApiShopServiceUpdate(adapter.getData().get(position).getId(), 3);
                                        }
                                    }, null);
                            break;
                    }

                }
            }
        };
        rvGoods.setOnItemMenuClickListener(mItemMenuClickListener);
        rvGoods.setAdapter(adapter);
        getStoreList();

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(activity, AddStoreActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("name1", name1);
                intent.putExtra("name2", name2);
                intent.putExtra("data", NotShelfGoodsActivity.this.adapter.getData().get(position));
                startActivity(intent);
            }
        });

    }

    @Override
    public int setLayout() {
        return R.layout.activity_not_shelf_goods;
    }

    boolean isFirst;

    @Override
    public void initLoadStart() {
        if (isFirst) {
            loading.show();
            isFirst = false;
        } else
            showDialog();
    }

    @Override
    public void initLoadEnd() {
        loading.dismiss();
        dialog.dismiss();
    }

    @Override
    public void initBindingView(Object mBean) {
        BindingViewBean bindingViewBean = (BindingViewBean) mBean;
        switch (bindingViewBean.getmEnumStatus()) {
            case 筛选获取商品列表:
                ApiShopServiceSelectAllBean selectAllBean = (ApiShopServiceSelectAllBean) bindingViewBean.getBean();
                if ("0000000".equals(selectAllBean.getCode())) {
                    adapter.setNewData(selectAllBean.getData().getList());
                } else {
                    adapter.setNewData(null);
                    Toasty.normal(activity, selectAllBean.getMsg()).show();
                }
                break;
            case 修改店铺商品信息:
                PubStatusBean pubStatusBean = (PubStatusBean) bindingViewBean.getBean();
                if ("0000000".equals(pubStatusBean.getCode())) {
                    getStoreList();
                }
                Toasty.normal(activity, pubStatusBean.getMsg()).show();

                break;
        }
    }

    @OnClick({R.id.ll_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
        }
    }

    void getStoreList() {
        businessDao.ApiShopServiceSelectAll(id, type1, type2, 2);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        getStoreList();
    }
}
